package com.student.Activitys.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.pl.wheelview.WheelView;
import com.student.Activitys.R;
import com.student.Activitys.utils.DataUtils;

import java.util.ArrayList;

/**
 * 项目名称：智慧校园学生端
 * 类描述：周课表 dialog
 * 创建人：张飞祥
 * 创建时间：2017/9/27 16:17
 * 修改人：张飞祥
 * 修改时间：2017/9/27 16:17
 * 修改备注：
 */
public abstract  class ScheduleWeekDialog extends Dialog implements View.OnClickListener {

    //取消
    TextView tvDialogExit;
    //确认
    TextView tvDialogConfirm;
    //学年学期
    WheelView wvDialogTerm;
    //周次
    WheelView wvDialogWeeks;


    private Activity activity;

    public ScheduleWeekDialog(@NonNull Activity activity) {
        super(activity, R.style.MyDialogTheme);
        this.activity = activity;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //初始化加载
        setContentView(R.layout.dialog_schedule_week);
        tvDialogExit = (TextView) findViewById(R.id.tv_dialog_exit);
        tvDialogConfirm = (TextView) findViewById(R.id.tv_dialog_confirm);
        wvDialogTerm = (WheelView) findViewById(R.id.wv_dialog_term);
        wvDialogWeeks = (WheelView) findViewById(R.id.wv_dialog_weeks);

        //点击事件监听
        tvDialogExit.setOnClickListener(this);
        tvDialogConfirm.setOnClickListener(this);

        //设置dialog 样式
        setViewLocation();
        //学年学期
        wvDialogTerm.setData(getTermList());
        wvDialogTerm.setEnable(true);
        wvDialogTerm.setDefault(2);
        //周次
        ArrayList<String> list = new ArrayList<>();
        for (int i = 1; i < 21; i++) {
            list.add(i+"");
        }
        wvDialogWeeks.setData(list);
        wvDialogWeeks.setEnable(true);
        wvDialogWeeks.setDefault(0);
    }

    /**
     * 设置dialog位于屏幕底部
     */
    private void setViewLocation(){
        DisplayMetrics dm = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
        int height = dm.heightPixels;

        Window window = this.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.x = 0;
        lp.y = height;
        lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
        lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        // 设置显示位置
        onWindowAttributesChanged(lp);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_dialog_exit:
                this.cancel();
                break;
            case R.id.tv_dialog_confirm:
                confirm(wvDialogTerm,wvDialogWeeks);
                this.cancel();
                break;
        }
    }

    public abstract void confirm(WheelView term,WheelView weeks);

    //获取学年学期数据源
    public ArrayList<String> getTermList() {
        ArrayList<String> list = new ArrayList<>();
        int date = Integer.valueOf(DataUtils.getNowDate("yyyy")) ;
        list.add((date-1)+"-"+date+"-"+1);
        list.add((date-1)+"-"+date+"-"+2);
        list.add(date+"-"+(date+1)+"-"+1);
        list.add(date+"-"+(date+1)+"-"+2);
        return list;
    }
}
